#!/bin/bash
#version 0.2
#Author empire
#Email wsg1107556314@163.com

#show the program graphic
echo "*********************************************************************"
echo "************* Welcome to empire's drcom login program **************"
echo "****************** Usage: drcom [integer TryTimes] ******************"
echo "************ Author: empire Email: wsg1107556314@163.com ************"

# get the pid of drcomd
drpid=$(pidof drcomd)
#record the output of command 'drcomc login'
loginInfo=''
#define retry times
declare -i tryTimes=9
if [ ! -z $1 ];then
	tryTimes=$1
fi
#if loginStatus > 0,then means has already login succeded.
declare -i loginStatus=0
#if hasLogin > 0,then means has already loggoe in,need to logout.
declare -i hasLogin=0
# if drcomd service is not running, start it,or else restart it.
if [ -z $drpid ];then
	echo "*********************************************************************"
	echo "** The service drcomd is not running,now is trying to start it ... **"
	echo "************************** Please wait ... **************************"
        drcomd
	loginInfo=$(drcomc login)
	hasLogin=$(echo -e $loginInfo | grep 'Error, Already logged in' | wc -c)
	loginStatus=$(echo -e $loginInfo | grep 'Login Succeeded' | wc -c)
else
	#make judgement whether has succeded logined,if not,retry the process(10 times limited) until success.
	echo "*********************************************************************"
	echo "The service drcomd is running, now is trying to check the network ..."
	echo "************************** Please wait ... **************************"
fi
#initial login times i as integer
declare -i i=0
#define the length of command 'ping www.bvadu.com'
declare -i connStatus=9
connStatus=$(ping -c 1 www.baidu.com | grep '100% packet loss' | wc -c)
if [ $connStatus -eq 0 ];then
	echo "**************** Your network is running normally *******************"
	echo "*********************************************************************"
	exit 0
fi
#do retry
while [ $loginStatus -eq 0 -a $connStatus -gt 0 ]
do
	#if the program has reached the max try times,then exit
	if [ $i -ge $tryTimes ]; then
		echo "**********************************************************************"
		echo "******** Login failed! Please check the network connection... ********"
		echo "**********************************************************************"
		exit 0
	fi
	i=$(expr $i+1)
	echo "     Taking the very effort to try. $i times has tried...     "
	#Already logged in,so,just to logout.
	if [ $hasLogin -gt 0 ]; then
		drcomc logout &> /dev/null
	fi	
	drpid=$(pidof drcomd)
        kill -9 $drpid &> /dev/null
	curIp=$(ifconfig | grep inet | grep -v 127.0.0.1 | grep -v inet6 | grep 218 | awk  'BEGIN {FS=":"}{print $2}' | awk '{print $1}')
	#not the correct ip
	if [ -z $curIp ];then
		/etc/init.d/networking restart
		dhclient -s 218.194.44.1
	fi
        drcomd
	drcomc login &> /dev/null
	drcomc logout &> /dev/null
	loginInfo=$(drcomc login)
        hasLogin=$(echo -e $loginInfo | grep 'Error, Already logged in' | wc -c)
        loginStatus=$(echo -e $loginInfo | grep 'Login Succeeded' | wc -c)
	connStatus=$(ping -c 1 www.baidu.com | grep '100% packet loss' | wc -c)
done
echo "************************** Login Succeded! **************************"
echo "*********************************************************************"
